/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camera;

import com.googlecode.javacv.FrameGrabber;
import coordinates.ConvexHull;
import java.util.logging.Level;
import java.util.logging.Logger;
import objectExtraction.HSVSkinDetector;
import utilities.FindContours;
import com.googlecode.javacv.VideoInputFrameGrabber;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class SkinBasedCamera extends Camera {

    FrameGrabber grabber = null;
    public IplImage hsvSkin = null, skinTemp = null, imTemp = null;
    HSVSkinDetector hd = new HSVSkinDetector();
    FindContours fc = new FindContours();
    ConvexHull ch = new ConvexHull();
    CvSeq largestCont = null;
    CvPoint fingerTip = null;
    public int rel_case;
    //  private int[] TOUCH_THRESHOLD = new int[2];

    public SkinBasedCamera(int camera, int fov, int IMAGE_NO) {
        super(camera, fov);
        this.IMAGE_NO = IMAGE_NO;
    }

    @Override
    public void run() {
        // setThreshold();
        grabber = new VideoInputFrameGrabber(CAMERA_NUM);
        System.out.println("Camera Num=" + CAMERA_NUM);
        int posX;
        try {
            grabber.start();
            Thread.sleep(1000);
            IplImage img = null;
            while (active) {
                img = grabber.grab();
                imTemp = img.clone();
                if (img != null) {
                    hsvSkin = hd.getHSVSkin(img);
                    skinTemp = hsvSkin.clone();
                    rel_case = fc.findLargeContours(hsvSkin);

//                    if (rel_case == 2) {
//                        largestCont = fc.getLargestContour();
//                        fingerTip = ch.getFingerTip(largestCont);
//
//                        if (fingerTip.y() > lastY) {
//
//                            cvDrawCircle(imTemp, fingerTip, 5, CvScalar.RED, -1, 8, 0);
//                            posX = fingerTip.x();
//                            y = fingerTip.y();
//                            theta = angFind.angleFinder(posX, hsvSkin.width(), CAMERA_FOV);
//                            cvClearSeq(largestCont);
//
////                            secondLargestCont = fc.getSecondLargestContour();
////                            fingerTip = ch.getFingerTip(secondLargestCont);
////                            cvDrawCircle(imTemp, fingerTip, 5, CvScalar.BLUE, -1, 8, 0);
////                            theta2 = angFind.angleFinder(posX, hsvSkin.width(), CAMERA_FOV);
//
//                        }
//
//                    } else
                    if (rel_case == 1) {

                        largestCont = fc.getLargestContour();
                        fingerTip = ch.getFingerTip(largestCont);
                        //System.out.println("Camera"+CAMERA_NUM+"  "+fingerTip.y());
                        if (fingerTip.y() > lastY) {
                            cvDrawCircle(imTemp, fingerTip, 15, CvScalar.RED, -1, 8, 0);
                            posX = fingerTip.x();
                            theta = angFind.angleFinder(posX, hsvSkin.width(), CAMERA_FOV);
                            cvClearSeq(largestCont);
                        } else {
//                            largestCont = fc.getLargestContour();
//                            cvClearSeq(largestCont);
                            theta = 0;
                        }
                    } else {
//                        largestCont = fc.getLargestContour();
//                        cvClearSeq(largestCont);
                        theta = 0;
                    }
                    cvReleaseImage(hsvSkin);
                }
            }
            grabber.stop();
        } catch (InterruptedException ex) {
            Logger.getLogger(SkinBasedCamera.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
            System.out.println("Exception in Skin Based Detector");
            e.printStackTrace();
        }
    }
//    public void setThreshold() {
//        TOUCH_THRESHOLD[0] = 0;
//        TOUCH_THRESHOLD[1] = 0;
//    }
}
